package test.kdonghwa;

import org.springframework.jms.support.converter.MessageConversionException;
import org.springframework.jms.support.converter.MessageConverter;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;

/**
 * Created with IntelliJ IDEA.
 * User: kdonghwa
 * Date: 12. 4. 13.
 * Time: 오후 4:51
 * To change this template use File | Settings | File Templates.
 */
public class MsgConverterImpl implements MessageConverter {
    @Override
    public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException {
        if ( !(object instanceof Msg) ) {
            throw new MessageConversionException("not Msg");
        }

        Msg msg = (Msg)object;
        MapMessage mapMessage = session.createMapMessage();

        mapMessage.setString("name", msg.getName());
        mapMessage.setString("value", msg.getValue());
        return mapMessage;

    }

    @Override
    public Object fromMessage(Message message) throws JMSException, MessageConversionException {
        if( !(message instanceof MapMessage)) {
            throw new MessageConversionException("not MapMessage");
        }
        MapMessage mapMessage = (MapMessage)message;
        Msg msg = new Msg();
        msg.setName(mapMessage.getString("name"));
        msg.setValue(mapMessage.getString("value"));

        return msg;
    }
}
